<template>
  <div class="order-management">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="header-left">
        <div class="logo">
          <span class="logo-icon">🛒</span>
          <span class="logo-text">社区团购管理系统</span>
        </div>
      </div>
      <div class="header-right">
        <span class="user-info">admin</span>
        <el-button type="text" @click="logout">退出</el-button>
      </div>
    </div>

    <div class="main-container">
      <!-- 左侧菜单 -->
      <div class="sidebar">
        <el-menu
          default-active="order-management"
          class="sidebar-menu"
          background-color="#f5f5f5"
          text-color="#333"
          active-text-color="#409EFF"
        >
          <el-menu-item index="dashboard">
            <span>首页概览</span>
          </el-menu-item>
          <el-menu-item index="order-management">
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="product-management">
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="user-management">
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="category-management">
            <span>分类管理</span>
          </el-menu-item>
          <el-menu-item index="statistics">
            <span>统计分析</span>
          </el-menu-item>
          <el-menu-item index="settings">
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 主要内容区域 -->
      <div class="content">
        <div class="content-header">
          <h2>订单管理</h2>
        </div>

        <!-- 搜索区域 -->
        <div class="search-section">
          <el-form :model="searchForm" inline class="search-form">
            <el-form-item label="订单号">
              <el-input
                v-model="searchForm.orderNumber"
                placeholder="请输入订单号"
                clearable
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item label="商品名称">
              <el-input
                v-model="searchForm.productName"
                placeholder="请输入商品名称"
                clearable
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item label="用户名称">
              <el-input
                v-model="searchForm.userName"
                placeholder="请输入用户名称"
                clearable
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item label="订单状态">
              <el-select
                v-model="searchForm.status"
                placeholder="请选择状态"
                clearable
                style="width: 150px"
              >
                <el-option label="待付款" value="pending" />
                <el-option label="已付款" value="paid" />
                <el-option label="已发货" value="shipped" />
                <el-option label="已完成" value="completed" />
                <el-option label="已取消" value="cancelled" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
              <el-button @click="handleReset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 数据表格 -->
        <div class="table-section">
          <el-table
            :data="tableData"
            style="width: 100%"
            border
            stripe
            v-loading="loading"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column prop="orderNumber" label="订单号" width="180" />
            <el-table-column prop="productName" label="商品名称" width="200" />
            <el-table-column prop="specification" label="规格型号" width="120" />
            <el-table-column prop="quantity" label="数量" width="80" align="center" />
            <el-table-column prop="productCode" label="作品编号" width="120" />
            <el-table-column prop="price" label="单价" width="100" align="right">
              <template #default="scope">
                ¥{{ scope.row.price }}
              </template>
            </el-table-column>
            <el-table-column prop="totalAmount" label="总金额" width="120" align="right">
              <template #default="scope">
                ¥{{ scope.row.totalAmount }}
              </template>
            </el-table-column>
            <el-table-column prop="orderTime" label="下单时间" width="180" />
            <el-table-column prop="status" label="订单状态" width="100" align="center">
              <template #default="scope">
                <el-tag
                  :type="getStatusType(scope.row.status)"
                  size="small"
                >
                  {{ getStatusText(scope.row.status) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="150" fixed="right">
              <template #default="scope">
                <el-button
                  type="primary"
                  size="small"
                  @click="handleView(scope.row)"
                >
                  查看
                </el-button>
                <el-button
                  type="success"
                  size="small"
                  @click="handleEdit(scope.row)"
                >
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 分页 -->
        <div class="pagination-section">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>

        <!-- 底部版权信息 -->
        <div class="footer">
          <p>Copyright © 2024 社区团购管理系统. All Rights Reserved.</p>
          <p>技术支持：Vue3 + Element Plus | 版本号：v1.0.0</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 响应式数据
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)

// 搜索表单
const searchForm = reactive({
  orderNumber: '',
  productName: '',
  userName: '',
  status: ''
})

// 表格数据
const tableData = ref([])

// 模拟数据
const mockData = [
  {
    id: 1,
    orderNumber: '202401171553007',
    productName: '新鲜苹果富士苹果红富士苹果新鲜水果',
    specification: '5斤/盒',
    quantity: 1,
    productCode: '1001',
    price: '39.90',
    totalAmount: '39.90',
    orderTime: '2024-01-17 15:53:07',
    status: 'paid',
    userName: '张三'
  },
  {
    id: 2,
    orderNumber: '202401171553008',
    productName: '新鲜苹果富士苹果红富士苹果新鲜水果',
    specification: '5斤/盒',
    quantity: 2,
    productCode: '1002',
    price: '39.90',
    totalAmount: '79.80',
    orderTime: '2024-01-17 15:53:08',
    status: 'shipped',
    userName: '李四'
  },
  {
    id: 3,
    orderNumber: '202401171553009',
    productName: '新鲜苹果富士苹果红富士苹果新鲜水果',
    specification: '5斤/盒',
    quantity: 1,
    productCode: '1003',
    price: '39.90',
    totalAmount: '39.90',
    orderTime: '2024-01-17 15:53:09',
    status: 'completed',
    userName: '王五'
  },
  {
    id: 4,
    orderNumber: '202401171553010',
    productName: '新鲜苹果富士苹果红富士苹果新鲜水果',
    specification: '5斤/盒',
    quantity: 3,
    productCode: '1004',
    price: '39.90',
    totalAmount: '119.70',
    orderTime: '2024-01-17 15:53:10',
    status: 'pending',
    userName: '赵六'
  },
  {
    id: 5,
    orderNumber: '202401171553011',
    productName: '新鲜苹果富士苹果红富士苹果新鲜水果',
    specification: '5斤/盒',
    quantity: 1,
    productCode: '1005',
    price: '39.90',
    totalAmount: '39.90',
    orderTime: '2024-01-17 15:53:11',
    status: 'cancelled',
    userName: '孙七'
  }
]

// 获取状态类型
const getStatusType = (status) => {
  const statusMap = {
    pending: 'warning',
    paid: 'primary',
    shipped: 'info',
    completed: 'success',
    cancelled: 'danger'
  }
  return statusMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    pending: '待付款',
    paid: '已付款',
    shipped: '已发货',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知'
}

// 搜索
const handleSearch = () => {
  loading.value = true
  // 模拟搜索延迟
  setTimeout(() => {
    loadData()
    loading.value = false
    ElMessage.success('搜索完成')
  }, 500)
}

// 重置
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  loadData()
}

// 查看订单
const handleView = (row) => {
  ElMessage.info(`查看订单：${row.orderNumber}`)
}

// 编辑订单
const handleEdit = (row) => {
  ElMessage.info(`编辑订单：${row.orderNumber}`)
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  loadData()
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  loadData()
}

// 退出登录
const logout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('退出成功')
  })
}

// 加载数据
const loadData = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    tableData.value = mockData
    total.value = mockData.length
    loading.value = false
  }, 300)
}

// 组件挂载时加载数据
onMounted(() => {
  loadData()
})
</script>

<style scoped>
.order-management {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部导航栏 */
.header {
  height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
}

.logo-icon {
  font-size: 24px;
  margin-right: 8px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-info {
  font-size: 14px;
}

/* 主容器 */
.main-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* 左侧菜单 */
.sidebar {
  width: 200px;
  background-color: #f5f5f5;
  border-right: 1px solid #e6e6e6;
}

.sidebar-menu {
  border: none;
  height: 100%;
}

.sidebar-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
}

/* 内容区域 */
.content {
  flex: 1;
  padding: 20px;
  background-color: #f8f9fa;
  overflow-y: auto;
}

.content-header {
  margin-bottom: 20px;
}

.content-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
}

/* 搜索区域 */
.search-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.search-form {
  margin: 0;
}

/* 表格区域 */
.table-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 分页区域 */
.pagination-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
}

/* 底部版权 */
.footer {
  background: white;
  padding: 20px;
  border-radius: 8px;
  text-align: center;
  color: #666;
  font-size: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.footer p {
  margin: 5px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 60px;
  }
  
  .sidebar-menu .el-menu-item span {
    display: none;
  }
  
  .search-form {
    flex-direction: column;
  }
  
  .search-form .el-form-item {
    margin-right: 0;
    margin-bottom: 10px;
  }
}

/* 表格样式优化 */
:deep(.el-table) {
  font-size: 14px;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #333;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 12px 0;
}

/* 按钮样式 */
.el-button {
  border-radius: 4px;
}

/* 标签样式 */
.el-tag {
  border-radius: 4px;
}
</style>